<!DOCTYPE html>
<html>
<head>
	<meta charset="UTF-8">
	<title>权限列表</title>
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<link rel="stylesheet" href="../../css/oksub.css">
	<script type="text/javascript" src="../../lib/loading/okLoading.js"></script>
</head>
<body class="ok-body-scroll">
<div class="ok-body">
	<!--面包屑导航区域-->
	<!--<div class="ok-body-breadcrumb">
					<span class="layui-breadcrumb">
							<a><cite>首页</cite></a>
							<a><cite>常用页面</cite></a>
							<a><cite>权限列表</cite></a>
					</span>
			<a class="layui-btn layui-btn-sm" href="javascript:location.replace(location.href);" title="刷新">
					<i class="layui-icon layui-icon-refresh"></i>
			</a>
	</div>
	&lt;!&ndash;模糊搜索区域&ndash;&gt;
	<div class="layui-row">
			<form class="layui-form layui-col-md12 ok-search">
					<input class="layui-input" placeholder="开始日期" autocomplete="off" id="startTime">
					<input class="layui-input" placeholder="截止日期" autocomplete="off" id="endTime">
					<input class="layui-input" placeholder="请输入权限名" autocomplete="off">
					<button class="layui-btn" lay-submit="" lay-filter="search">
							<i class="layui-icon layui-icon-search"></i>
					</button>
			</form>
	</div>-->
	<div class="layui-row">
		<div class="layui-col-md3">
			<div id="permissionTree"></div>
		</div>
		<div class="layui-col-md9">
			<table class="layui-hide" id="tableId" lay-filter="tableFilter"></table>
		</div>
	</div>
</div>
<!--js逻辑-->
<script src="../../lib/layui/layui.js"></script>
<script>
	var rowData;
	layui.use(["element", "table", "laydate", "tree", "okUtils", "okMock",'okLayer'], function () {
		let table = layui.table;
		let laydate = layui.laydate;
		let tree = layui.tree;
		let okUtils = layui.okUtils;
		let okMock = layui.okMock;
		let okLayer=layui.okLayer;
		okLoading.close();
		laydate.render({elem: '#startTime', type: "datetime"});
		laydate.render({elem: '#endTime', type: "datetime"});

        table.on("toolbar(tableFilter)", function (obj) {
            switch (obj.event) {
                case "add":
                    add();
                    break;
                case "batchDel":
                    batchDel();
                    break;
            }
        });

        table.on("tool(tableFilter)", function (obj) {
            let data = obj.data;
            switch (obj.event) {
                case "edit":
                    rowData=data;
                    edit(data.id);
                    break;
                case "del":
                    del(data.id);
                    break;
            }
        });


		okUtils.ajax("/sysPermission/tree", "get", null, true).done(function (response) {
			tree.render({
				elem: '#permissionTree',
				data: response.data,
				showCheckbox: true,
				id: 'id',
				isJump: false,
				click: function (obj) {
					var data = obj.data;
					layer.msg('状态：' + obj.state + '<br>节点数据：' + JSON.stringify(data));
				}
			});

			initPermissionTable();
		}).fail(function (error) {
			console.log(error)
		});

        let permissionTable;
		function initPermissionTable() {
            permissionTable=table.render({
				elem: '#tableId',
				url: "/sysPermission/list",
				limit: 20,
				page: true,
				size: "sm",
                toolbar: "#toolbarTpl",
				cols: [[
                    {type: "checkbox"},
					{field: "id", title: "ID", width: 60, sort: true},
					{field: "title", title: "权限名称", width: 100},
					{field: "code", title: "权限标识", width: 150},
					{field: "href", title: "权限路径", width: 150},
					{field:'parentPermission',title:'父菜单',width: 150,align: 'center',templet:function(d){
                            if(d.parentPermission == null){
                                return '无';
                            }else{
                                return d.parentPermission.title;
                            }
					}},
					{field: "type", title: "类型", width: 100, templet: "#typeTpl"},
                    {field: "status", title: "状态", width: 85, templet: "#statusTpl"},
					{title: "操作", width: 100, templet: "#operationTpl", align: "center", fixed: "right"}
				]],
				done: function (res, curr, count) {
					console.info(res, curr, count);
				},
                request: {
                    pageName: 'page', //页码的参数名称，默认：page
                    limitName: 'size' //每页数据量的参数名，默认：limit
                },
                response: {
                    statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
                },
                parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                    return {
                        "code": res.status, //解析接口状态
                        "msg": res.message, //解析提示文本
                        "count": res.data.total, //解析数据长度
                        "data": res.data.records //解析数据列表
                    };
                }
			});
		}

        function add() {
            okLayer.open("添加权限", "permission-add", "90%", "90%", null, function () {
                permissionTable.reload();
            })
        }
        function edit() {
            okLayer.open("修改权限", "permission-edit", "90%", "90%", null, function () {
                permissionTable.reload();
            })
        }

        function del(id) {
            okLayer.confirm("确定要删除吗？", function () {
                okUtils.ajax("/sysPermission/del", "get", {ids: id}, true).done(function (response) {
                    okUtils.tableSuccessMsg(response.message);
                }).fail(function (error) {
                    console.log(error)
                });
            })
        }

        function batchDel() {
            okLayer.confirm("确定要批量删除吗？", function (index) {
                layer.close(index);
                let idsStr = okUtils.tableBatchCheck(table);
                if (idsStr) {
                    okUtils.ajax("/sysPermission/del", "get", {ids: idsStr}, true).done(function (response) {
                        okUtils.tableSuccessMsg(response.message);
                    }).fail(function (error) {
                        console.log(error);
                    });
                }
            });
        }



	});
</script>
<!-- 头工具栏模板 -->
<script type="text/html" id="toolbarTpl">
	<div class="layui-btn-container">
		<div class="layui-inline" lay-event="add"><i class="layui-icon layui-icon-add-1"></i></div>
		<div class="layui-inline" lay-event="batchDel"><i class="layui-icon layui-icon-delete"></i></div>
	</div>
</script>
<!--行工具栏模板-->
<script type="text/html" id="operationTpl">
	<a href="javascript:" title="编辑" lay-event="edit"><i class="layui-icon">&#xe642;</i></a>
	<a href="javascript:" title="删除" lay-event="del"><i class="layui-icon">&#xe640;</i></a>
</script>

<script type="text/html" id="typeTpl">
	{{#  if(d.type == 0){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">菜单</span>
	{{#  } else if(d.type == 1){ }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">功能</span>

	{{#  } }}
</script>
<script type="text/html" id="statusTpl">
	{{#  if(d.status == 0){ }}
	<span class="layui-btn layui-btn-normal layui-btn-xs">启用</span>
	{{#  } else if(d.status == 1) { }}
	<span class="layui-btn layui-btn-warm layui-btn-xs">停用</span>
	{{#  } else if(d.status == 2) { }}
	<span class="layui-btn layui-btn-danger layui-btn-xs">删除</span>
	{{#  } }}
</script>
</body>
</html>
